package tankgame6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;

//坦克大战的绘图区
public class MyPanel extends JPanel implements KeyListener, Runnable {
    Hero hero = null;

    //定义敌人坦克，放入vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    //定义一个存放Node对象的Vector，用于恢复敌人坦克的坐标和方向
    Vector<Node> nodes = new Vector<>();


    int enemyTankSize = 3;

    //定义一个Vector，用于存放炸弹
    Vector<Bomb> bombs = new Vector<>();
    //定义三张炸弹图片用于显示爆炸效果
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public MyPanel(String key) {
        //先判断记录文件是否存在
        //如果存在就正常执行，不存在则提示：开启新游戏，key = "1"
        File file = new File(Recorder.getRecordFile());
        if (!file.exists()){
            System.out.println("文件不存在，开始新的游戏");
            key = "1";
        }

        //将MyPanel对象的enemyTanks设置给Recorder中的enemyTanks
        Recorder.setEnemyTanks(enemyTanks);

        hero = new Hero(500, 100);//初始化我的坦克
        hero.setSpeed(2);

        switch (key){
            case "1":

                //初始化敌人坦克
                for (int i = 0; i < enemyTankSize; i++) {
                    EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);
                    //将enemyTanks传给enemyTank对象
                    enemyTank.setEnemyTanks(enemyTanks);
                    new Thread(enemyTank).start();
                    enemyTank.setDirect(2);
                    //给enemyTank加入子弹
                    Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
                    enemyTank.shots.add(shot);
                    //启动shot对象
                    new Thread(shot).start();
                    enemyTanks.add(enemyTank);
                }
                break;
            case "2"://继续上局游戏
                //进行数据恢复
                nodes = Recorder.getNodesAndEnemyTanksRec();

                //初始化敌人坦克
                for (int i = 0; i < nodes.size(); i++) {
                    Node node = nodes.get(i);
                    EnemyTank enemyTank = new EnemyTank(node.getX(), node.getY());
                    //将enemyTanks传给enemyTank对象
                    enemyTank.setEnemyTanks(enemyTanks);
                    new Thread(enemyTank).start();
                    enemyTank.setDirect(node.getDirect());
                    //给enemyTank加入子弹
                    Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
                    enemyTank.shots.add(shot);
                    //启动shot对象
                    new Thread(shot).start();
                    enemyTanks.add(enemyTank);
                }
                break;
            default:
                System.out.println("输入有误");
        }


        //初始化图片对象
        image1 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb1.png"));
        System.out.println(MyPanel.class);
        image2 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb1.png"));
        image3 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb1.png"));

    }
    //编写方法，显示我方击毁敌方坦克的信息
    public void showInfo(Graphics g){
        //画出玩家的总成绩
        g.setColor(Color.BLACK);
        Font font= new Font("宋体",Font.BOLD,25);
        g.setFont(font);

        g.drawString("累计击毁敌方坦克：",1020,30);
        drawTank(1020,60,g,0,0);
        g.setColor(Color.BLACK);
        g.drawString(Recorder.getAllEnemyTankNum()+"",1080,100);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);

        //画出击毁坦克数
        showInfo(g);

        //画出坦克-封装方法
        if (hero!= null && hero.isLive) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
        }

        //画坦克的子弹
        //遍历hero的shots集合，实现多颗子弹的发射
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.isLive) {
//            g.fill3DRect(hero.shot.x,hero.shot.y,1,1,false);
                g.draw3DRect(shot.x, shot.y, 2, 2, false);
            }else {//如果shot对象那个已经销毁则从集合去掉
                hero.shots.remove(shot);

            }
        }

        //如果bombs集合中有对象就画出
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
            } else if (bomb.life > 3) {
                g.drawImage(image1, bomb.x, bomb.y, 40, 40, this);
            } else {
                g.drawImage(image1, bomb.x, bomb.y, 20, 20, this);
            }
            //让坦克生命不断减少
            bomb.lifeDown();
            //如果bomb life为0，就从bombs集合中删除
            if (bomb.life == 0) {
                bombs.remove(bomb);
            }
        }

        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            //先判断敌方坦克是否存活再绘制
            if (enemyTank.isLive) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    Shot shot = enemyTank.shots.get(j);
                    if (shot.isLive) {
                        g.draw3DRect(shot.x, shot.y, 2, 2, false);
                    } else {
                        enemyTank.shots.remove(j);
                    }
                }
            }
        }
    }

    //编写方法，画出坦克

    /**
     * @param x      坦克左上角x坐标
     * @param y      左上角y坐标
     * @param g      画笔
     * @param direct 坦克方向
     * @param type   坦克类型
     */
    public void drawTank(int x, int y, Graphics g, int direct, int type) {


        switch (type) {
            case 0://我方坦克
                g.setColor(Color.cyan);
                break;
            case 1://地方坦克
                g.setColor(Color.yellow);
                break;


        }
        //根据坦克的方向来绘制坦克
        switch (direct) {
            case 0://向上
                g.fill3DRect(x, y, 10, 60, false);//左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//中间
                g.fillOval(x + 10, y + 20, 20, 20);//圆
                g.drawLine(x + 20, y, x + 20, y + 30);
                break;
            case 1://向右
                g.fill3DRect(x, y, 60, 10, false);//左边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//中间
                g.fillOval(x + 20, y + 10, 20, 20);//圆
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;

            case 2://向上
                g.fill3DRect(x, y, 10, 60, false);//左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//中间
                g.fillOval(x + 10, y + 20, 20, 20);//圆
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;

            case 3://向左
                g.fill3DRect(x, y, 60, 10, false);//左边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//中间
                g.fillOval(x + 20, y + 10, 20, 20);//圆
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;

        }
    }

    public void hitEnemyTank(){
        for (int i = 0; i <hero.shots.size() ; i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.isLive) {
                for (int j = 0; j < enemyTanks.size(); j++) {
                    EnemyTank enemyTank = enemyTanks.get(j);
                    hitTank(shot, enemyTank);
                }
            }

        }
    }

    //编写方法，判断敌方坦克是否击中我方坦克
    public void hitHero(){
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            for(int j = 0;j<enemyTank.shots.size();j++){
                //取出子弹
                Shot shot = enemyTank.shots.get(j);
                //判断shot是否击中我方坦克
                if(hero.isLive && shot.isLive){
                    hitTank(shot,hero);
                }
            }
        }
    }

    // 编写方法判断我方子弹是否击中敌方坦克
    public void hitTank(Shot s, Tank enemyTank) {
        switch (enemyTank.getDirect()) {
            case 0://上
            case 2://下
                if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 40 && s.y > enemyTank.getY()
                        && s.y < enemyTank.getY() + 60) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    enemyTanks.remove(enemyTank);
                    //击毁敌方坦克计数++
                    if (enemyTank instanceof EnemyTank){
                        Recorder.addAllEnemyTankNum();
                    }
                    //创建Bomb对象,加入到Bomb集合
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                }
                break;
            case 1://上
            case 3://下
                if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 60 && s.y > enemyTank.getY()
                        && s.y < enemyTank.getY() + 40) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    enemyTanks.remove(enemyTank);
                    if (enemyTank instanceof EnemyTank){
                        Recorder.addAllEnemyTankNum();
                    }

                    //创建Bomb对象,加入到Bomb集合
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                }
                break;
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirect(0);
            if (hero.getY() > 0) {
                hero.moveUp();
            }
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirect(1);
            if (hero.getX() + 60 < 1000) {
                hero.moveRight();
            }

        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirect(2);
            if (hero.getY() + 60 < 750) {
                hero.moveDown();
            }
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirect(3);
            if (hero.getX() > 0) {
                hero.moveLeft();
            }
        }

        if (e.getKeyCode() == KeyEvent.VK_J) {
            //如果按下的时J,就发射子弹
            hero.shotEnemyTank();

//            //判断hero的子弹是否销毁
//            if (hero.shot == null || !hero.shot.isLive) {
//                hero.shotEnemyTank();
//            }
        }

        //让面板重绘
        this.repaint();


    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {//每隔100毫秒重绘一次
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            //判断是否击中了敌人坦克
            hitEnemyTank();
            //判断敌人是否击中我们
            hitHero();
            this.repaint();
        }
    }
}
